//package com.michaelbulava.TFS.vcs.internal.providers;
//
//import com.intellij.history.core.changes.ChangeSet;
//import com.intellij.openapi.progress.ProcessCanceledException;
//import com.intellij.openapi.project.Project;
//import com.intellij.openapi.util.Pair;
//import com.intellij.openapi.vcs.*;
//import com.intellij.openapi.vcs.actions.VcsContextFactory;
//import com.intellij.openapi.vcs.changes.committed.DecoratorManager;
//import com.intellij.openapi.vcs.changes.committed.VcsCommittedListsZipper;
//import com.intellij.openapi.vcs.changes.committed.VcsCommittedViewAuxiliary;
//import com.intellij.openapi.vcs.history.VcsRevisionNumber;
//import com.intellij.openapi.vcs.versionBrowser.ChangeBrowserSettings;
//import com.intellij.openapi.vcs.versionBrowser.ChangesBrowserSettingsEditor;
//import com.intellij.openapi.vcs.versionBrowser.CommittedChangeList;
//import com.intellij.openapi.vfs.VirtualFile;
//import com.intellij.util.AsynchConsumer;
//import com.michaelbulava.TFS.Messages;
//import com.michaelbulava.TFS.vcs.TFSVcs;
//import com.michaelbulava.TFS.vcs.exceptions.NotConnectedToTFS;
//import com.michaelbulava.TFS.vcs.exceptions.TFSException;
//import com.michaelbulava.TFS.vcs.internal.TFSRepositoryLocation;
//import com.michaelbulava.TFS.vcs.internal.WorkstationHelper;
//import com.michaelbulava.TFS.vcs.internal.providers.internal.TFSChangeList;
//import com.michaelbulava.TFS.vcs.ui.TFSVersionFilterComponent;
//import com.michaelbulava.TFS.vcs.utils.TFSFileUtil;
//import com.microsoft.tfs.core.clients.versioncontrol.VersionControlClient;
//import com.microsoft.tfs.core.clients.versioncontrol.VersionControlConstants;
//import com.microsoft.tfs.core.clients.versioncontrol.soapextensions.*;
//import com.microsoft.tfs.core.clients.versioncontrol.specs.ItemSpec;
//import com.microsoft.tfs.core.clients.versioncontrol.specs.version.ChangesetVersionSpec;
//import com.microsoft.tfs.core.clients.versioncontrol.specs.version.DateVersionSpec;
//import com.microsoft.tfs.core.clients.versioncontrol.specs.version.LatestVersionSpec;
//import com.microsoft.tfs.core.clients.versioncontrol.specs.version.VersionSpec;
//import org.jetbrains.annotations.Nls;
//import org.jetbrains.annotations.NotNull;
//import org.jetbrains.annotations.Nullable;
//
//import java.io.DataInput;
//import java.io.DataOutput;
//import java.io.IOException;
//import java.util.*;
//
///**
// * Created by MichaelA on 8/17/2015.
// */
//public class TFSCommitedChangedProvider
//        implements CachingCommittedChangesProvider<TFSChangeList, ChangeBrowserSettings>{
//
//    private final TFSVcs myVcs;
//
//    public TFSCommitedChangedProvider(TFSVcs vcs){
//        myVcs = vcs;
//    }
//
//    public TFSCommitedChangedProvider(Project project){
//        myVcs = TFSVcs.getInstance(project);
//    }
//
//    @Override
//    public int getFormatVersion() {
//        return 1;
//    }
//
//    @Override
//    public void writeChangeList(DataOutput stream, TFSChangeList list) throws IOException {
//        list.writeToStream(stream);
//    }
//
//    @Override
//    public TFSChangeList readChangeList(RepositoryLocation location, DataInput stream) throws IOException {
//        return new TFSChangeList(this.myVcs, stream);
//    }
//
//    @Override
//    public boolean isMaxCountSupported() {
//        return true;
//    }
//
//    @Nullable
//    @Override
//    public Collection<FilePath> getIncomingFiles(RepositoryLocation repositoryLocation) throws VcsException {
//        return null;
//    }
//
//    @Override
//    public boolean refreshCacheByNumber() {
//        return true;
//    }
//
//    @Nls
//    @Nullable
//    @Override
//    public String getChangelistTitle() {
//        return Messages.getString("TFSCommittedChangesProvider.ChangelistTitle");
//    }
//
//    @Override
//    public boolean isChangeLocallyAvailable(FilePath filePath, @Nullable VcsRevisionNumber localRevision,
//                                            VcsRevisionNumber changeRevision, TFSChangeList changeList) {
//        return (localRevision != null) && (localRevision.compareTo(changeRevision) >= 0);
//    }
//
//    @Override
//    public boolean refreshIncomingWithCommitted() {
//        return false;
//    }
//
//    @NotNull
//    @Override
//    public ChangeBrowserSettings createDefaultSettings() {
//        return new ChangeBrowserSettings();
//    }
//
//    @Override
//    public ChangesBrowserSettingsEditor<ChangeBrowserSettings> createFilterUI(boolean showDateFilter) {
//        return new TFSVersionFilterComponent(showDateFilter);
//    }
//
//    @Nullable
//    @Override
//    public RepositoryLocation getLocationFor(FilePath root) {
//        final Map<Workspace, List<FilePath>> pathsByWorkspaces = new HashMap<>();
//        try{
//            WorkstationHelper.processByWorkspaces(Collections.singletonList(root), true, myVcs.getProject(),
//                    new WorkstationHelper.VoidDelegateProcess() {
//                        @Override
//                        public void executeRequest(Workspace workspace, List<FilePath> localPaths) throws TFSException {
//                            pathsByWorkspaces.put(workspace, localPaths);
//                        }
//                    });
//            if (!pathsByWorkspaces.isEmpty())
//                return new TFSRepositoryLocation(pathsByWorkspaces);
//        } catch (NotConnectedToTFS e){
//            return null;
//        } catch (TFSException e) {
//            AbstractVcsHelper.getInstance(myVcs.getProject()).showError(new VcsException(e.getMessage(), e), TFSVcs.VCS_NAME);
//        } catch(ProcessCanceledException e){
//            AbstractVcsHelper.getInstance(myVcs.getProject()).showError(new VcsException(Messages
//                    .getString("TFSCommittedChangesProvider.CanceledByUser"), e),TFSVcs.VCS_NAME);
//        }
//        return null;
//    }
//
//    @Nullable
//    @Override
//    public RepositoryLocation getLocationFor(FilePath root, String repositoryPath) {
//        return getLocationFor(root);
//    }
//
//    @Nullable
//    @Override
//    public VcsCommittedListsZipper getZipper() {
//        return null;
//    }
//
//    @Override
//    public List<TFSChangeList> getCommittedChanges(ChangeBrowserSettings settings, RepositoryLocation location,
//                                                   int maxCount) throws VcsException {
//        final List<TFSChangeList> result = new ArrayList<>();
//        loadCommittedChanges(settings, location, maxCount, new AsynchConsumer<CommittedChangeList>() {
//            @Override
//            public void finished() {}
//
//            @Override
//            public void consume(CommittedChangeList committedChangeList) {
//                result.add((TFSChangeList) committedChangeList);
//            }
//        });
//        return result;
//    }
//
//    @Override
//    public void loadCommittedChanges(ChangeBrowserSettings settings, RepositoryLocation location,
//                                     int maxCount, AsynchConsumer<CommittedChangeList> consumer) throws VcsException {
//        VersionSpec versionFrom = new ChangesetVersionSpec(1);
//        if (settings.getChangeAfterFilter() != null)
//            versionFrom = new ChangesetVersionSpec(settings.getChangeAfterFilter().intValue());
//        if (settings.getDateAfterFilter() != null){
//            Calendar c = Calendar.getInstance();
//            c.setTime(settings.getDateAfterFilter());
//            versionFrom = new DateVersionSpec(c);
//        }
//        VersionSpec versionTo = LatestVersionSpec.INSTANCE;
//        if (settings.getChangeBeforeFilter() != null)
//            versionTo = new ChangesetVersionSpec(settings.getChangeBeforeFilter().intValue());
//        if (settings.getDateBeforeFilter() != null){
//            Calendar c = Calendar.getInstance();
//            c.setTime(settings.getDateBeforeFilter());
//            versionTo = new DateVersionSpec(c);
//        }
//
//        TFSRepositoryLocation tfsRepositoryLocation = (TFSRepositoryLocation) location;
//        try{
//            for (Map.Entry<Workspace, List<FilePath>> entry : tfsRepositoryLocation.getPathsByWorkspaces().entrySet()){
//                Workspace workspace = entry.getKey();
//                Map<FilePath, ExtendedItem> extendedItemMap = TFSFileUtil.getFilePathExtendedItemMap(myVcs.getProject(),
//                        workspace, entry.getValue());
//                for(Map.Entry<FilePath, ExtendedItem> localPath2ExtendedItem : extendedItemMap.entrySet()){
//                    ExtendedItem extendedItem = (ExtendedItem)localPath2ExtendedItem.getValue();
//                    if (extendedItem != null){
//                        int itemLastestVersion = getLatestChangesetId(workspace, settings.getUserFilter(), extendedItem);
//                        if(versionFrom instanceof ChangesetVersionSpec){
//                            ChangesetVersionSpec changesetVersionSpecFrom = (ChangesetVersionSpec)versionFrom;
//                            if (changesetVersionSpecFrom.getChangeset() > itemLastestVersion){
//                                //TODO ????
//                            }
//                        } else {
//                            if (versionTo instanceof  ChangesetVersionSpec){
//                                ChangesetVersionSpec changesetVersionTo = (ChangesetVersionSpec)versionTo;
//                                if (changesetVersionTo.getChangeset() > itemLastestVersion)
//                                    versionTo = new ChangesetVersionSpec(itemLastestVersion);
//                            }
//                            VersionSpec itemVersion = LatestVersionSpec.INSTANCE;
//                            Changeset[] changesets = workspace.getClient().queryHistory(extendedItem.getTargetServerItem(),
//                                    itemVersion, extendedItem.getDeletionID(), RecursionType.FULL, null, versionFrom,
//                                    versionTo, maxCount, true, true, true, false);
//                            for(int i = 0; i < changesets.length; i++){
//                                TFSChangeList newlist = new TFSChangeList(workspace, changesets[i].getChangesetID(),
//                                        changesets[i].getOwner(), changesets[i].getDate().getTime(),
//                                        changesets[i].getComment(), myVcs);
//                                consumer.consume(newlist);
//                            }
//                        }
//                    }
//                }
//            }
//        } catch(Throwable e){
//          throw new VcsException(e);
//        } finally {
//            consumer.finished();
//        }
//    }
//
//    @Override
//    public ChangeListColumn[] getColumns() {
//        return new ChangeListColumn[] {
//                new ChangeListColumn.ChangeListNumberColumn(Messages.getString("TFSCommittedChangesProvider.RevisionColumn")),
//                ChangeListColumn.NAME,
//                ChangeListColumn.DATE,
//                ChangeListColumn.DESCRIPTION
//        };
//    }
//
//    @Nullable
//    @Override
//    public VcsCommittedViewAuxiliary createActions(DecoratorManager decoratorManager, RepositoryLocation repositoryLocation) {
//        return null;
//    }
//
//    @Override
//    public int getUnlimitedCountValue() {
//        return 0;
//    }
//
//    @Nullable
//    @Override
//    public Pair<TFSChangeList, FilePath> getOneList(VirtualFile file, VcsRevisionNumber number) throws VcsException {
//        ChangeBrowserSettings settings = createDefaultSettings();
//        settings.USE_CHANGE_AFTER_FILTER = true;
//        settings.USE_CHANGE_BEFORE_FILTER = true;
//        settings.CHANGE_BEFORE = (settings.CHANGE_AFTER = String.valueOf(number.asString()));
//        FilePath filePath = VcsContextFactory.SERVICE.getInstance().createFilePathOn(file);
//        List<TFSChangeList> list = getCommittedChanges(settings, getLocationFor(filePath), 1);
//        if (list.size() == 1) {
//            return new Pair(list.get(0), filePath);
//        }
//        return null;
//    }
//
//    @Override
//    public RepositoryLocation getForNonLocal(VirtualFile virtualFile) {
//        return null;
//    }
//
//    @Override
//    public boolean supportsIncomingChanges() {
//        return true;
//    }
//
//
//    private int getLatestChangesetId(Workspace workspace, String user, ExtendedItem extendedItem) {
//        if (extendedItem.getItemType() == ItemType.FILE)
//            return extendedItem.getLatestVersion();
//        VersionSpec itemVersion = LatestVersionSpec.INSTANCE;
//        VersionSpec versionFrom = new ChangesetVersionSpec(1);
//        VersionSpec versionTo = LatestVersionSpec.INSTANCE;
//        int maxCount = 1;
//        ItemSpec itemSpec = TFSFileUtil.createItemSpec(extendedItem.getSourceServerItem(), RecursionType.FULL);
//        Changeset[] changeSets = workspace.getClient().queryHistory(extendedItem.getTargetServerItem(), itemVersion, extendedItem.getDeletionID(),
//                RecursionType.FULL, null, versionFrom, versionTo, maxCount, true, true, true, false);
//        return changeSets[0].getChangesetID();
//    }
//
//}
